package cn.xuewei.controller;

import cn.xuewei.mapper.SessionListMapper;
import cn.xuewei.mapper.UserMapper;
import cn.xuewei.pojo.entity.SessionList;
import cn.xuewei.pojo.vo.UserChatVO;
import cn.xuewei.response.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/v1/sessionList/")
public class SessionListController {

    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired(required = false)
    private SessionListMapper sessionListMapper;

    /**
     * 已建立会话
     * 通过id查询是否已经建立对话
     */
    @GetMapping("already")
    public ResultVO sessionListAlready(@RequestParam Integer id){
        List<SessionList> sessionLists = sessionListMapper.selectByUserId(id);
        System.out.println("sessionListAlready()被调用");
        return ResultVO.ok(sessionLists);
    }

    /**
     * 获取用户头像
     */
    @GetMapping("imgUrl")
    public ResultVO sessionListGetImgUrl(@RequestParam Integer id){
        System.out.println("getImgUrl()被调用");
        return ResultVO.ok(userMapper.selectChatVoByPrimaryKey(id));
    }

    /**
     * 可建立会话的用户(未建立会话的用户)
     * 通过查询数据库中没有建立,来获取可建立的会话
     */
    @GetMapping("not")
    public ResultVO sessionListNot(@RequestParam Integer id){
        List<Integer> list = sessionListMapper.selectToUserIdByUserId(id);
        list.add(id);
        List<UserChatVO> cloudList = userMapper.getCloudList(list);
        System.out.println("getSessionListNot()被调用");
        return ResultVO.ok(cloudList);
    }

    @GetMapping("getSession")
    public ResultVO getSession(@RequestParam Integer sessionId){
        return ResultVO.ok(sessionListMapper.selectByPrimaryKey(sessionId));
    }

    /**
     * 建立会话
     * @param id 当前用户
     * @param toUserId 目标用户
     * @return 返回一条消息
     */
    @GetMapping("create")
    public ResultVO createSession(@RequestParam Integer id, @RequestParam Integer toUserId){
        SessionList sessionList = new SessionList();

        //给所属用户赋值
        UserChatVO user = userMapper.selectChatVoByPrimaryKey(id);
        sessionList.setUserId(id);
        sessionList.setUserName(user.getUserName());
        sessionList.setUserImgUrl(user.getImgUrl());

        //给到用户赋值
        UserChatVO toUser = userMapper.selectChatVoByPrimaryKey(toUserId);
        sessionList.setToUserId(toUserId);
        sessionList.setToUserName(toUser.getUserName());
        sessionList.setToUserImgUrl(toUser.getImgUrl());

        //第一次创建会话,因此设置未读消息为 0
        sessionList.setUnReadCount(0);
        sessionListMapper.insert(sessionList);

        //因为会话是双向的,所以无论是那一边想要对话,都需要为对方创建会话
        //判断对方是否已经与当前用户建立会话 没有也要建立
        //从数据库中查找是否以及有对方建立的会话
        Integer integer = sessionListMapper.selectByUser(toUserId,id);
        //如果查询不到或者查询到的id<= 0  则说明了数据库中没有会话,需要建立会话
        if(integer == null || integer <= 0){

            UserChatVO user1 = userMapper.selectChatVoByPrimaryKey(toUserId);
            sessionList.setUserId(toUserId);
            sessionList.setUserName(user1.getUserName());
            sessionList.setUserImgUrl(user1.getImgUrl());

            //给到用户赋值
            UserChatVO toUser1 = userMapper.selectChatVoByPrimaryKey(id);
            sessionList.setToUserId(id);
            sessionList.setToUserName(toUser1.getUserName());
            sessionList.setToUserImgUrl(toUser1.getImgUrl());
            sessionListMapper.insert(sessionList);
        }
        return ResultVO.ok();
    }

    /**
     * 通过会话Id删除会话
     * @param sessionId 会话id
     */
    @GetMapping("delect")
    public ResultVO delectSession(@RequestParam Integer sessionId){
        sessionListMapper.deleteByPrimaryKey(sessionId);
        return ResultVO.ok();
    }
}
